﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using LeadVinaglue.Helpers;
using System.Data;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Repository
{
    public class ArchiveManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        #region Model view
        public ArchiveManagementModel GetModel(string pageType, string roleName)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListArchive = GetAll();

            return model;
        }

        public ArchiveManagementModel GetModelAdd(string pageType, string roleName)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListArchiveType = new Repository.ArchiveTypeManagementRepository().GetAll();

            return model;
        }

        public ArchiveManagementModel GetModelEdit(string pageType, string roleName, int id)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListArchiveType = new Repository.ArchiveTypeManagementRepository().GetAll();

            if (id > 0)
            {
                model.ArchiveInfo = GetSingle(id);
            }

            return model;
        }

        public ArchiveManagementModel GetModelDelete(string pageType, string roleName)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();
            model.ListArchive = GetAll();

            return model;
        }

        public ArchiveManagementModel GetModelbyArchiveTypeID(int ArchiveTypeID)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();
            model.ListArchive = GetAllbyId(ArchiveTypeID);

            return model;
        }
        #endregion

        #region Archive

        public IQueryable<Archive> GetAll(bool active)
        {
            return (from c in entities.Archives where (c.Active == active) orderby c.DateCreated descending select c);
        }

        public List<Models.ArchiveModel> GetAll()
        {
            List<Models.ArchiveModel> list = new List<Models.ArchiveModel>();

            using (SqlDataReader item = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.Text, "SELECT * FROM [vwArchive] ORDER BY [ID] DESC"))
            {
                while (item.Read())
                {
                    ArchiveModel model = new ArchiveModel();

                    model.ID = item.GetInt32(0);
                    model.Title = item.GetString(1);
                    if (!item.IsDBNull(2))
                        model.Ordered = item.GetInt32(2);
                    if (!item.IsDBNull(3))
                    {
                        model.DateCreated = Convert.ToDateTime(item.GetDateTime(3)).ToString("yyyy-MM-dd hh:mm tt");
                        model.DateOrdered = Convert.ToDateTime(item.GetDateTime(3));
                    }
                    model.Active = item.GetBoolean(4);
                    model.ArchiveTypeId = item.GetInt32(5);
                    model.ArchiveTypeName = item.GetString(6);

                    list.Add(model);
                }
            }

            return list;
        }

        public Archive GetSingle(int id)
        {
            return (from c in entities.Archives where c.ID == id select c).FirstOrDefault();
        }

        public void Create(Archive _object)
        {
            try
            {
                _object.DateCreated = System.DateTime.Now;
                entities.AddToArchives(_object);
                entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void Save(Archive _object)
        {
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Archives where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
        public List<Models.ArchiveModel> GetAllbyId(int ArchiveTypeID)
        {
            List<Models.ArchiveModel> list = new List<Models.ArchiveModel>();

            using (SqlDataReader item = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.Text, "SELECT * FROM [vwArchive] where ArchiveTypeID = " + ArchiveTypeID.ToString() + " ORDER BY [ID] ASC"))
            {
                while (item.Read())
                {
                    ArchiveModel model = new ArchiveModel();

                    model.ID = item.GetInt32(0);
                    model.Title = item.GetString(1);
                    if (!item.IsDBNull(2))
                        model.Ordered = item.GetInt32(2);
                    if (!item.IsDBNull(3))
                    {
                        model.DateCreated = Convert.ToDateTime(item.GetDateTime(3)).ToString("yyyy-MM-dd hh:mm tt");
                        model.DateOrdered = Convert.ToDateTime(item.GetDateTime(3));
                    }
                    model.Active = item.GetBoolean(4);
                    model.ArchiveTypeId = item.GetInt32(5);
                    model.ArchiveTypeName = item.GetString(6);
                    model.LongDesc = item.GetString(7);
                    if (!item.IsDBNull(8))
                    {
                        model.ImageURL = item.GetString(8);
                    }
                    list.Add(model);
                }
            }

            return list;
        }
        #endregion
    }
}